import store from '@/store/index.js'
const token = uni.getStorageSync('token') || ''
 const beforeRequest = (options) => {	
	options.ourl =  options.url
	options.url = this.$config.apiUrl + options.url
	console.log(this.$config.apiUrl)
	options.method = options.method || 'GET'
	options.timeout = options.timeout
	options.header = {
		'Authorization': `Bearer ${token}`
		}
	return options
}
 const afterRequest = (result) => {
	return new Promise((resolve, rej) => {
		let status;
		if ([100,-100].includes(result.data.Code) ) {
			if([-100].includes(result.data.Code)){
				 rej(result.data)
				 uni.showToast({
				 	icon:'none',
				 	title:result.data.Msg||'请求失败'
				 })
				 return
			}
			if(result.data.Msg){
				uni.showToast({
					icon:'none',
					title:result.data.Msg||'请求成功'
				})
			}
		
			resolve(result.data)
		}else {
			rej(result.data)
		}

	})

}
export const allRquest = (options) => {
	return new Promise(async (resolve, reject) => {
		const res = beforeRequest(options)
		if (res) {
			try {
				const result = await uni.request(res)
				console.log('result', result);
			if(result){
				const data = await afterRequest(result)
				if (data) {
					resolve(data)
				} else {
					resolve('code非200')
				}
	setTimeout(() => {
		uni.hideLoading()
		}, 300)
		
			}
		

			} catch (error) {
	setTimeout(() => {
		uni.hideLoading()
	}, 300)
		
				reject(error)
				//TODO handle the exception
			}


		} else {

			reject('没有token且不在白名单')
		}

	})
}